package io.github.generator.strategy;

import com.baomidou.mybatisplus.generator.config.po.TableField;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import io.github.common.consts.StringPool;
import io.github.common.util.StringUtils;
import io.github.generator.engine.ApplicationVelocityEngine;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 枚举文件注入策略
 *
 * @author ZH.Team
 * @version 1.0.0
 * @email "mailto:ZH.Team@zhxx.com"
 * @date 2024.11.21 21:15
 * @since 1.0.0
 */
@AllArgsConstructor
public class EnumFileInjectionStrategy {

    /**
     * Path
     */
    private final String path;

    /**
     * Package path
     */
    private final String packagePath;

    private final ApplicationVelocityEngine velocityEngine;

    public void outputFile(TableInfo tableInfo, Map<String, Object> objectMap) {
        List<TableField> tableFields = new LinkedList<>();
        for (TableField field : tableInfo.getFields()) {
            String comment = field.getComment();
            if (StringUtils.isEmpty(comment)) {
                return;
            }
            //使用自定义枚举
            boolean customEnumerations = StringUtils.startsWith(comment, "自定义枚举");
            //使用系统枚举
            boolean systemEnumerations =  StringUtils.startsWith(comment, "系统枚举");

            if (customEnumerations || systemEnumerations) {
                EnumConfig config = new EnumConfig();

                String[] split = StringUtils.split(comment, StringPool.COLON);
                String enumName = split[1];
                config.setEnumFileName(enumName);

                String content = split[2];
                if (StringUtils.isNotBlank(comment)) {
                    List<String> list = Arrays.asList(StringUtils.split(content, StringPool.SEMICOLON));
                    config.setContent(list);
                }
            } else {
                tableFields.add(field);
            }
        }
    }

    /**
     * <p>Company: 深圳振瀚信息技术有限公司成都分公司 </p>
     * <p>Description: </p>
     *
     * @author ZH.Team
     * @version 1.0.0
     * @email "mailto:ZH.Team@zhxx.com"
     * @date 2024.11.21 21:05
     * @since 1.0.0
     */
    @Data
    public static class EnumConfig {

        /**
         * Enum package
         */
        private String enumPackage;

        /**
         * 枚举内容
         */
        private List<String> content;

        /**
         * Enum names
         */
        private String enumFileName;

        /**
         * Enum field name
         */
        private String enumFieldName;
    }
}
